import { list, add, edit, del } from '@/api/trainFeel'

const state = {
  feelList: [],
  loading: false,
  total: 0
}

const mutations = {
  SET_FEEL_LIST: (state, list) => {
    state.feelList = list
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  SET_TOTAL: (state, total) => {
    state.total = total
  },
  ADD_FEEL: (state, feel) => {
    state.feelList.push(feel)
  },
  UPDATE_FEEL: (state, updatedFeel) => {
    const index = state.feelList.findIndex(item => item.id === updatedFeel.id)
    if (index !== -1) {
      state.feelList.splice(index, 1, updatedFeel)
    }
  },
  DELETE_FEEL: (state, id) => {
    const index = state.feelList.findIndex(item => item.id === id)
    if (index !== -1) {
      state.feelList.splice(index, 1)
    }
  }
}

const actions = {
  // 获取感受列表
  getFeelList({ commit }, params = { current: 1, size: 100 }) {
    commit('SET_LOADING', true)
    return new Promise((resolve, reject) => {
      list(params).then(response => {
        const { content, totalElements } = response
        commit('SET_FEEL_LIST', content)
        commit('SET_TOTAL', totalElements)
        commit('SET_LOADING', false)
        resolve(response)
      }).catch(error => {
        commit('SET_LOADING', false)
        reject(error)
      })
    })
  },

  // 新增感受
  addFeel({ commit }, data) {
    return new Promise((resolve, reject) => {
      add(data).then(response => {
        commit('ADD_FEEL', response)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 修改感受
  editFeel({ commit }, data) {
    return new Promise((resolve, reject) => {
      edit(data).then(response => {
        commit('UPDATE_FEEL', response)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 删除感受
  deleteFeel({ commit }, ids) {
    return new Promise((resolve, reject) => {
      del(ids).then(response => {
        // 如果是单个删除
        if (typeof ids === 'number') {
          commit('DELETE_FEEL', ids)
        } else if (Array.isArray(ids)) {
          // 如果是批量删除
          ids.forEach(id => {
            commit('DELETE_FEEL', id)
          })
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 刷新感受列表
  refreshFeelList({ dispatch }) {
    return dispatch('getFeelList')
  },

  // 监听 CRUD 操作并自动同步数据
  syncFeelData({ dispatch }, { action, data, response }) {
    switch (action) {
      case 'add':
        return dispatch('addFeel', response)
      case 'edit':
        return dispatch('editFeel', response)
      case 'del':
        return dispatch('deleteFeel', data.id)
      case 'list':
        return dispatch('getFeelList', data)
      default:
        return Promise.resolve()
    }
  }
}

const getters = {
  feelList: state => state.feelList,
  feelLoading: state => state.loading,
  feelTotal: state => state.total,
  // 根据ID获取感受名称
  getFeelNameById: (state) => (id) => {
    const feel = state.feelList.find(item => item.id === id)
    return feel ? feel.name : ''
  },
  // 根据ID获取感受对象
  getFeelById: (state) => (id) => {
    return state.feelList.find(item => item.id === id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 